<?php

namespace App\Http\Controllers\Api;

use App\Models\Apply;
use App\Models\Record;
use App\Models\Staff;
use App\Models\Visitor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ApplyController extends BaseController
{
    /**
     * 获取预约列表
     */
    public function lists(Request $request) {
        try{
            $current_date = date('Y-m-d');
            $items = Apply::where('visitor_id', $request->visitor->id)
                    ->where('visit_date', '>=', $current_date)
                    ->orderBy('visit_date', 'desc')->orderBy('visit_time', 'desc')
                    ->get();
            foreach($items as $key => $val) {
                $items[$key]->status_title = Apply::$_status[$val->status];
            }
            return $this->response(200, '获取成功', ['items' => $items]);
        }catch (\Throwable $e) {
            return $this->response(500, $e->getMessage());
        }
    }
    /**
     * 初始化预约登记数据
     */
    public function init() {
        try{
            $staffs = Staff::select(['id', 'name'])->get();
            $visit_start_date = date('Y-m-d', time() + 3600 * 24);
            return $this->response(200, '获取成功', ['staffs' => $staffs, 'visit_start_date' => $visit_start_date]);
        }catch (\Throwable $e) {
            return $this->response(500, $e->getMessage());
        }
    }
    /**
     * 预约登记
     */
    public function add(Request $request) {
        try{
            $staff_id = $request->input('staff_id', 0);
            if (empty($staff_id)) return $this->response(400, '请选择被访者');
            $visit_date = $request->input('visit_date', '');
            if (empty($visit_date)) return $this->response(400, '请选择来访日期');
            $visit_time = $request->input('visit_time', '');
            if (empty($visit_time)) return $this->response(400, '请选择来访时间');
            $reason = $request->input('reason', '');
            if (empty($reason)) return $this->response(400, '请填写来访事由');
            $partners = $request->input('partners', '');
            $data = [
                'visitor_id' => $request->visitor->id,
                'staff_id' => $staff_id,
                'visit_date' => $visit_date,
                'visit_time' => $visit_time,
                'reason' => $reason,
                'partners' => $partners
            ];
            if (!Apply::create($data)) return $this->response(400, '预约失败');
            return $this->response(200, '预约成功');
        }catch (\Throwable $e) {
            return $this->response(500, $e->getMessage());
        }
    }

    /**
     * 取消预约
     */
    public function cancel(Request  $request) {
        try{
            $apply_id = $request->input('apply_id', 0);
            if (empty($apply_id)) return $this->response(400, '缺少预约ID参数');
            if (!Apply::where('id', $apply_id)->update(['status' => 4])) {
                return $this->response(400, '操作失败');
            }
            return $this->response(200, '操作成功');
        }catch (\Throwable $e) {
            return $this->response(500, $e->getMessage());
        }
    }

    /**
     * 扫码获取预约信息
     */
    public function info(Request $request) {
        try{
            $apply_id = $request->input('apply_id', 0);
            if (empty($apply_id)) return $this->response(400, '缺少预约ID参数');
            $apply = Apply::where('id', $apply_id)->first();
            $apply->visitor = Visitor::where('id', $apply->visitor_id)->first();
            $apply->staff = Staff::where('id', $apply->staff_id)->first();
            return $this->response(200, '操作成功', $apply);
        }catch (\Throwable $e) {
            return $this->response(500, $e->getMessage());
        }
    }
    /**
     * 到访登记
     */
    public function comeRegister(Request $request) {
        try{
            $apply_id = $request->input('apply_id', 0);
            if (empty($apply_id)) return $this->response(400, '缺少预约ID参数');
            $apply = Apply::where('id', $apply_id)->first();
            if ($apply->visit_date != date('Y-m-d')) return $this->response(400, '该预约到访日期不是今天');
            if($apply->status != 2) return $this->response(400, '该预约状态禁止到访登记');
            DB::beginTransaction();
            $data = ['status' => 5];
            if (!Apply::where('id', $apply_id)->update($data)) {
                DB::rollBack();
                return $this->response(400, '到访状态更新失败');
            }
            $data = [
                'apply_id' => $apply_id,
                'type' => 1
            ];
            if(!Record::create($data)) {
                DB::rollBack();
                return $this->response(400, '到访记录创建失败');
            }
            DB::commit();
            return $this->response(200, '操作成功', $apply);
        }catch (\Throwable $e) {
            return $this->response(500, $e->getMessage());
        }
    }
    /**
     * 离访登记
     */
    public function goRegister(Request $request) {
        try{
            $apply_id = $request->input('apply_id', 0);
            if (empty($apply_id)) return $this->response(400, '缺少预约ID参数');
            $apply = Apply::where('id', $apply_id)->first();
            if($apply->status != 5) return $this->response(400, '该预约状态禁止离访登记');
            DB::beginTransaction();
            $data = ['status' => 6];
            if (!Apply::where('id', $apply_id)->update($data)) {
                DB::rollBack();
                return $this->response(400, '离访状态更新失败');
            }
            $data = [
                'apply_id' => $apply_id,
                'type' => 2
            ];
            if(!Record::create($data)) {
                DB::rollBack();
                return $this->response(400, '离访记录创建失败');
            }
            DB::commit();
            return $this->response(200, '操作成功', $apply);
        }catch (\Throwable $e) {
            return $this->response(500, $e->getMessage());
        }
    }
}
